package com.genius.framework.multitenancy.rest;

import com.genius.framework.multitenancy.entity.DynamicTask;
import com.genius.framework.multitenancy.service.ScheduleTaskClientService;
import com.genius.framework.multitenancy.service.ScheduleTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

@RequestMapping("/task")
@RestController
public class ScheduleTaskResource {

    @Autowired
    private ScheduleTaskService scheduleTaskService;

    @Autowired
    private ScheduleTaskClientService scheduleTaskClientService;

    /**
     * 启动定时任务
     * TODO 可以将添加和启动分开，添加到数据库，启动在添加到任务中
     * @param dynamicTask
     * @return 任务的key
     */
    @PostMapping("/start")
    public String withParams(@RequestBody DynamicTask dynamicTask) {
        return scheduleTaskService.addTask(dynamicTask);
    }

    /**
     * 执行一次任务
     * @param dynamicTask
     */
    @PostMapping("/execute")
    public void startImmediately(@RequestBody DynamicTask dynamicTask) {
        scheduleTaskService.executeTask(dynamicTask);
    }

    /**
     * 停止定时任务
     * @param taskKey
     */
    @DeleteMapping("/stop/{taskKey}")
    public void stopTask(@PathVariable("taskKey") String taskKey) {
        scheduleTaskService.stopTask(taskKey);
    }

    /**
     * 获取动态配置定时任务的类
     * @return
     */
    @GetMapping("/class")
    public List<Map<String, String>> getCommonScheduleTaskBean() {
        return scheduleTaskService.getCommonScheduleTaskBean();
    }
    /**
     * 获取动态配置定时任务类中的task方法
     * @param className 类名
     * @return
     */
    @GetMapping("/method/{className}")
    public List<Map<String, Object>> getCommonScheduleTaskMethod(@PathVariable String className) {
        return scheduleTaskService.getCommonScheduleTaskMethod(className);
    }

    @GetMapping("/class/service")
    public List<Map<String, String>> getCommonScheduleTaskBean(@RequestParam String serviceName) throws URISyntaxException {
        return scheduleTaskClientService.getCommonScheduleTaskBean(serviceName);
    }

    @GetMapping("/class/service/method")
    public List<Map<String, Object>> getCommonScheduleTaskMethod(@RequestParam String serviceName, @RequestParam String className) throws URISyntaxException {
        return scheduleTaskClientService.getCommonScheduleTaskMethod(serviceName, className);
    }
}
